package com.situation.controller;

import com.situation.annotation.ControllerEndpoint;
import com.github.pagehelper.PageInfo;
import com.situation.pojo.DO.TbLoginLog;
import com.situation.pojo.VO.UserVO;
import com.situation.service.LoginLogService;
import com.situation.util.RespUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/api/loginLog")
public class LoginLogController {

    @Autowired
    private LoginLogService loginLogService;

    @ControllerEndpoint(exceptionMessage = "删除登录日志失败",operation = "删除登录日志")
    /**
     * 删除单个           已测
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions({"loginLog:delete"})
    public RespUtil delete(@PathVariable("id") Long id) {
        int i = loginLogService.delete(id);
        if (i > 0)
            return RespUtil.ok();
        return RespUtil.fail();
    }

    @ControllerEndpoint(exceptionMessage = "批量删除登录日志失败",operation = "批量删除登录日志")
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/batchDelete/{ids}")
    @RequiresPermissions({"loginLog:batchDelete"})
    public RespUtil delete(@PathVariable("ids") String ids) {
        String[] idList = ids.split(",");
        List<Long> longList = Stream.of(idList).map(Long::parseLong).collect(Collectors.toList());
        int i = loginLogService.batchDelete(longList);
        if (i == longList.size())
            return RespUtil.ok();
        return RespUtil.fail();
    }

    /**
     * 多条件查询    模糊查询
     * @param pageNum
     * @param pageSize
     * @param loginLog
     * @return
     */
    @GetMapping("/findLoginLogList")
    public RespUtil findLoginLogList(int pageNum,int pageSize,TbLoginLog loginLog) {
        PageInfo<TbLoginLog> all = loginLogService.findAll(pageNum, pageSize, loginLog);
        return RespUtil.ok().setData(all);
    }

    /**
     * 登陆报表
     *
     * @param userVO
     * @return 20天以内登陆的人，按日期分
     *              me     某一天我登录次数
     *              all    某一天总登录次数
     */
    @PostMapping("/loginReport")
    public RespUtil loginReport(UserVO userVO){
        List<Map<String,Object>> mapList= loginLogService.loginReport(userVO);
        Map<String,Object> map=new HashMap<>();
        userVO.setUsername(null);
        List<Map<String,Object>> meList= loginLogService.loginReport(userVO);
        map.put("me",mapList);
        map.put("all",meList);
        return RespUtil.ok().setData(map);
    }


}
